home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Shout3Ddemo / Shout3d_runtime / codebase / applets / BindingTestPanel.java < prev    next >
Text File  |  2000-09-01  |  5KB  |  183 lines

  1. /**    
  2.     Company:        Eyematic Interfaces
  3.     Project:        Shout3D 2.0 Sample Code
  4.     Class:            BindingTestPanel
  5.     Date:            April 26, 1999
  6.     Description:    Class for panel that shows a test for binding viewpoints & backgrounds
  7.     (C) Copyright Eyematic Interfaces, Inc. - 1997-2000 - All rights reserved
  8.  */
  9. package applets;
  10.  
  11. import java.applet.*;
  12. import java.awt.*;
  13. import java.awt.image.*;
  14. import java.io.*;
  15. import java.util.Date;
  16. import java.net.URL;
  17. import shout3d.*;
  18. import shout3d.core.*;
  19.  
  20. /**
  21.  * Panel that tests the changing of viewpoint and background bindings.
  22.  * 
  23.  * Press the 'v' key to cycle through three viewpoints.
  24.  * The change is made just by setting the isBound field of
  25.  * the viewpoints and letting the library's mechanism respond
  26.  * 
  27.  * Press the 'b' key to change between 4 backgrounds, also through 
  28.  * the binding mechanism
  29.  * 
  30.  * @author Paul Isaacs
  31.  * @author Jim Stewartson
  32.  * @author Dave Westwood
  33.  */
  34.  
  35. public class BindingTestPanel extends Shout3DPanel implements DeviceObserver {
  36.     
  37.     // The viewpoint and background nodes that will be bound
  38.     Viewpoint vp0, vp1, vp2;
  39.     Background bk0, bk1, bk2, bk3;
  40.  
  41.     //{{ Shout3DApplet methods
  42.     /**
  43.      *  Constructor
  44.      */
  45.     public BindingTestPanel(Shout3DApplet applet){
  46.         super(applet);
  47.     }        
  48.         
  49.     /**
  50.      *
  51.      * This method is automatically called by the parent class Shout3DPanel
  52.      * at the correct time during initialize().
  53.      * 
  54.      * Subclasses should implement this to perform any custom initialization tasks.
  55.      */
  56.     public void customInitialize() {
  57.         
  58.         // Register to observe the keyboard
  59.         addDeviceObserver(this,"KeyboardInput", null);
  60.         
  61.         // Create 3 viewpoints and bind to the first
  62.         createViewpointNodes();
  63.         vp0.isBound.setValue(true);
  64.         
  65.         // Create 4 backgrounds and bind to the first
  66.         createBackgroundNodes();
  67.         bk0.isBound.setValue(true);
  68.     }
  69.  
  70.     /**
  71.      *  Finalize
  72.      */
  73.     protected void finalize() throws Throwable { 
  74.         // Clean up by unregistering observers
  75.         removeDeviceObserver(this,"KeyboardInput");
  76.         super.finalize();
  77.     }
  78.     //}} Shout3DApplet methods
  79.     
  80.     
  81.     //{{ DeviceObserver methods
  82.     /**
  83.      * 
  84.      * When keyboard input is received:
  85.      * 
  86.      * If 'v' or 'V' cycles through viewpoints
  87.      * If 'b' or 'B' cycles through backgrounds
  88.      */
  89.     public boolean onDeviceInput(DeviceInput di, Object userData) {
  90.         if (!(di instanceof KeyboardInput))
  91.             return false;
  92.         
  93.         KeyboardInput ki = (KeyboardInput) di;
  94.         if (ki.which == KeyboardInput.PRESS) {
  95.             if (ki.key == 'v' || ki.key == 'V') {
  96.                 // V key pressed. Cycle to next viewpoint.
  97.                 Viewpoint curVP = (Viewpoint)getCurrentBindableNode("Viewpoint");
  98.  
  99.                 if (curVP == vp0)
  100.                     vp1.isBound.setValue(true);
  101.                 else if (curVP == vp1)
  102.                     vp2.isBound.setValue(true);
  103.                 else if (curVP == vp2)
  104.                     vp0.isBound.setValue(true);
  105.                 return true;
  106.             }
  107.             if (ki.key == 'b' || ki.key == 'B') {
  108.                 // B key pressed. Toggle between backgrounds
  109.                 Background curBG = (Background) getCurrentBindableNode("Background");
  110.  
  111.                 if (curBG == bk0)
  112.                     bk1.isBound.setValue(true);
  113.                 else if (curBG == bk1)
  114.                     bk2.isBound.setValue(true);
  115.                 else if (curBG == bk2)
  116.                     bk3.isBound.setValue(true);
  117.                 else
  118.                     bk0.isBound.setValue(true);
  119.                 return true;
  120.             }
  121.         }
  122.         return false;
  123.     }
  124.     //}} DeviceObserver methods
  125.  
  126.     // Field values for the viewpoints 
  127.     float[] pos0 = { 0, 10, -75 };
  128.     float[] pos1 = { 40, 10, -40 };
  129.     float[] pos2 = { 0, 50, -10 };
  130.     float[] rot0 = { 0, 1, 0, 3.14159f };
  131.     float[] rot1 = { 0, 1, 0, 2.36f };
  132.     float[] rot2 = { 1, 0, 0, -1.57079f };
  133.     
  134.     void createViewpointNodes() {
  135.         // Create three viewpoints, add to the scene, 
  136.         // and give them different POVs.
  137.         vp0 = new Viewpoint(this);
  138.         vp1 = new Viewpoint(this);
  139.         vp2 = new Viewpoint(this);
  140.         vp0.isBound.setValue(false);
  141.         vp1.isBound.setValue(false);
  142.         vp2.isBound.setValue(false);
  143.         Node[] newKids = new Node[3];
  144.         newKids[0] = vp0;
  145.         newKids[1] = vp1;
  146.         newKids[2] = vp2;
  147.         if ((getScene() != null) && (getScene() instanceof Group)) {
  148.             ((Group)getScene()).addChildren(newKids);
  149.         }
  150.         else {
  151.             throw new Shout3DException("BindingTestPanel got null scene");
  152.         }
  153.     
  154.         // Set positions and orientations
  155.         vp0.position.setValue(pos0);
  156.         vp1.position.setValue(pos1);
  157.         vp2.position.setValue(pos2);
  158.         vp0.orientation.setValue(rot0);
  159.         vp1.orientation.setValue(rot1);
  160.         vp2.orientation.setValue(rot2);
  161.     }
  162.  
  163.     void createBackgroundNodes(){
  164.         // bk0 is the starting background, bk1 is one we make right here
  165.         bk0 = (Background) getCurrentBindableNode("Background");
  166.         
  167.         ImageTexture bkTexture = new ImageTexture(applet);
  168.         String[] urlVals1 = {"images/shared/sky_starry_bg.gif" };
  169.         bkTexture.url.setValue(urlVals1 );
  170.         bk1 = new Background(this);
  171.         bk1.texture.setValue(bkTexture);
  172.         
  173.         ImageTexture bkTexture2 = new ImageTexture(applet);
  174.         String[] urlVals2 = {"images/shared/partly_transparent_bg.gif"};
  175.         bkTexture2.url.setValue(urlVals2);
  176.         bk2 = new Background(this);
  177.         bk2.texture.setValue(bkTexture2);
  178.         
  179.         bk3 = new Background(this);
  180.         bk3.texture.setValue(bkTexture2);
  181.         bk3.stretchToFit.setValue(true);
  182.     }
  183. }